Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate towhat policy field #18032

Merged
merged 3 commits into from
Sep 28, 2018

Conversation

jvlcek
Copy link
Member

@jvlcek jvlcek commented Sep 28, 2018

https://bugzilla.redhat.com/show_bug.cgi?id=1435780

Invalid values for policy field towhat are not recognized, result in the policy being
created using the API.

The validations for mode were added in [PR 14519](#14519

The list of valid values for towhat were arrived at by examination of what is supported via
the UI tree_builder_policy.rb

Steps for Testing/QA

Attempt to create a policy with the following data:

    {
      "action": "create",  
      "resources": [{
        "name": "test policy bobsyouruncle",
        "description": "Desc bobsyouruncle",
        "mode": "control",
        "towhat": "bobsyouruncle",      
        "conditions_ids": [ 2000, 3000 ],
        "policy_contents": [{             
          "event_id": 2,
          "actions": [ {"action_id": 1, "opts": { "qualifier": "failure" } } ]
        }]
      }]
    }

e.g.:

$MY_MIQ_REPO/manageiq/tools/rest_api.rb  -l https://10.8.99.50 get /api/policies

<when prompted enter>

        {
          "action": "create",  
          "resources": [{
            "name": "test policy bobsyouruncle",
            "description": "Desc bobsyouruncle",
            "mode": "control",
            "towhat": "bobsyouruncle",      
            "conditions_ids": [ 2000, 3000 ],
            "policy_contents": [{             
              "event_id": 2,
              "actions": [ {"action_id": 1, "opts": { "qualifier": "failure" } } ]
            }]
          }]
        }

@jvlcek
Copy link
Member Author

jvlcek commented Sep 28, 2018

@miq-bot add_label bug

@jvlcek
Copy link
Member Author

jvlcek commented Sep 28, 2018

@miq-bot assign @bdunne

@jvlcek
Copy link
Member Author

jvlcek commented Sep 28, 2018

@miq-bot add_label hammer/yes

@@ -1,6 +1,16 @@
# TODO: Import/Export support

class MiqPolicy < ApplicationRecord
TOWHAT_APPLIES_TO_CLASSES = %w(Host
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be sorted alphabetically?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 will do


context '.validates' do
it 'validates towhat and mode' do
expect(described_class.create!(:towhat => "Host",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to save the record to test this, you can

expect(FactoryGirl.build(:miq_policy, :towhat => "Host", :mode => "compliance").valid?).to be true

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should mode even be tested here since it didn't change in this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm usually not a huge fan of this syntax, but you could also

expect(FactoryGirl.build(:miq_policy, :towhat => "Host")).to be_valid

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdunne Thank you for the input. I'll have the new test focus on towhat

I like the syntax the way it is and if you don't have strong feelings about it I'd like to leave it as is, mostly because it's more consistent with the tests through the spec.

Let me know if you are OK with leaving the test as is.

Thank you. JoeV

Copy link
Member

@bdunne bdunne Sep 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I don't see the point of writing to the database if we just want to make sure the instance is valid. Also, using FactoryGirl makes it much more obvious that you're only testing :towhat and not any other validation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the question is: are you testing that it's valid or testing if it raises an exception. If you're testing validity: you don't need to create the record, you can do as @bdunne suggests. If you're testing exception raising, you need to create!

It looks like your testing validity so create! isn't needed here. It's minor and I know other code does this but it's fine to mix patterns in the specs until you can convert the rest to the new way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdunne and @jrafanie Ah I see the reasoning now. Thank you for explaining. Will do.

JoeV

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I prefer expecting the specific thing (validity) rather than the side effect of rails raising an error when calling create! on an invalid instance.


it 'reports invalid towhat' do
expect do
described_class.create!(:towhat => "BobsYourUncle",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar here

expect(FactoryGirl.build(:miq_policy, :towhat => "BobsYourUncle")).not_to be_valid

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdunne Here again, if you feel strongly I'd be glad to change this test but for me it's much easier to quickly grasp what the test is doing, that a particular exception is expected and it's more consistent with existing tests in the spec.

Let me know if you are OK with it as is.

Thank you for the review feedback.
JoeV

Copy link
Member

@jrafanie jrafanie Sep 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be ok with testing create! if you're concerned with a custom validation message but since this is out of the box validation messages, I think you can just do thing.new + thing.valid? and maybe even thing.errors[:towhat].blank? or something like that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrafanie Thank you for explaining. Will do.

JoeV

@jvlcek
Copy link
Member Author

jvlcek commented Sep 28, 2018

@bdunne and @jrafanie Now that I see the result I appreciate the simplicity of this version of the code!
Thank you for your input.

JoeV

@miq-bot
Copy link
Member

miq-bot commented Sep 28, 2018

Checked commits jvlcek/manageiq@d344e43~...a9bdbd9 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 0 offenses detected
Everything looks fine. 🍰

@bdunne bdunne merged commit 5270416 into ManageIQ:master Sep 28, 2018
@bdunne bdunne added this to the Sprint 96 Ending Oct 8, 2018 milestone Sep 28, 2018
@jvlcek jvlcek deleted the bz_1435780_tow_hat_validations branch October 1, 2018 11:36
simaishi pushed a commit that referenced this pull request Oct 1, 2018
@simaishi
Copy link
Contributor

simaishi commented Oct 1, 2018

Hammer backport details:

$ git log -1
commit 28ab0177beb315a791af3d60a7ca5ae1cfc5c29a
Author: Brandon Dunne <brandondunne@hotmail.com>
Date:   Fri Sep 28 14:38:24 2018 -0400

    Merge pull request #18032 from jvlcek/bz_1435780_tow_hat_validations
    
    Validate towhat policy field
    
    (cherry picked from commit 52704166f4dc48ccc2ced8c6bac0e8001e8d786f)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1435780

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants